package com.jeomo.mem.provider.web;

import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.mem.provider.request.MemIndexRequest;
import com.jeomo.mem.provider.vo.MemIndexVo;
import com.jeomo.mem.starter.entity.MemIndexEntity;
import com.jeomo.mem.starter.service.IMemPointsSupplementService;
import com.jeomo.mem.starter.service.IMemberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>首页注册量controller</p>
 *
 * @author : 崔贺
 * @date : 2020-11-18 15:55
 **/
@RestController
@RequestMapping("/member/index")
public class MemberIndexController extends BaseController {

    @Resource
    private IMemberService memberService;
    @Resource
    private IMemPointsSupplementService memPointsSupplementService;

    @PostMapping("/tendency")
    public CommonResult<MemIndexVo> getIndexTendency(@RequestBody MemIndexRequest memIndexRequest) {
        MemIndexVo memIndexVo = new MemIndexVo();
        LocalDate dateFrom = memIndexRequest.getDateFrom();
        LocalDate dateTo = memIndexRequest.getDateTo();
        List<MemIndexEntity> memIndexEntities = memberService.queryRegisterTendency(dateFrom, dateTo);
        memIndexVo.setRegisterTendency(memIndexEntities);
        if (!dateFrom.equals(dateTo)) {
            MemIndexEntity memIndexEntity = memIndexEntities.get(memIndexEntities.size() - 1);
            memIndexEntities.remove(memIndexEntities.size() - 1);
            memIndexVo.setIntraDayCountRegister(memIndexEntity.getMemCount());
        } else {
            int countRegister = memberService.queryDayCountRegister(dateFrom);
            int oneDayCount = memberService.queryOneDayCountRegister(dateFrom);
            memIndexVo.setDayCountRegister(countRegister);
            memIndexVo.setIntraDayCountRegister(oneDayCount);
        }
        return CommonResult.success(memIndexVo);
    }

    @GetMapping("/toDo")
    public CommonResult<Integer> getToDoSum() {
        return CommonResult.success(memPointsSupplementService.queryToDoPhotographIntegral());
    }
}
